
<?php

 global $_RQ, $_AR;
        $rid     = intval($_RQ['rid']);
        $uniacid = $_AR['uniacid'];
        
        
        //开始抽奖咯
		$reply=pdo_get('jy_h5game_reply',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
        if ($reply == false) {
            $this->message(array(
                "success" => 2,
                "msg" => '规则出错！...'
            ), "");
        }
        
        if ($reply['isshow'] != 1) {
            //活动已经暂停,请稍后...
            $this->message(array(
                "success" => 2,
                "msg" => '活动暂停，请稍后...'
            ), "");
        }
        
        if (strtotime($reply['starttime']) > time()) {
            $this->message(array(
                "success" => 2,
                "msg" => '活动还没有开始呢'
            ), "");
        }
        
        if (strtotime($reply['endtime']) < time()) {
            $this->message(array(
                "success" => 2,
                "msg" => '活动已经结束了！'
            ), "");
        }
        
        if (empty($_AR['fans'])) {
            $this->message(array(
                "success" => 2,
                "msg" => '您还未关注公共账号！'
            ), "");
        }
        //先判断有没有资格领取		
        $fansID    = $_AR['member']['uid'];
        $from_user = $_AR['fans']['from_user'];
        //判断是否为关注用户
        $follow    = pdo_fetchcolumn("select follow from " . tablename('mc_mapping_fans') . " where openid=:openid and uniacid=:uniacid order by `fanid` desc", array(
            ":openid" => $from_user,
            ":uniacid" => $uniacid
        ));
        if ($follow == 0) {
            $this->message(array(
                "success" => 2,
                "msg" => '您还未关注公共账号！'
            ), "");
        }
        
        
        //判断是否为关注用户
        $fans = pdo_fetch("select * from " . tablename('haoman_bigwheel_fans') . " where rid = " . $rid . " and fansID=" . $fansID . " and from_user='" . $from_user . "'");
        
        if ($fans == false) {
            //不存在false的情况，如果是false，则表明是非法
            //$this->message();
            $fanss = array(
                'rid' => $rid,
                'uniacid' => $uniacid,
                'fansID' => $fansID,
                'from_user' => $from_user,
                'todaynum' => 0,
                'totalnum' => 0,
                'awardnum' => 0,
                'createtime' => time()
            );
            
            pdo_insert('haoman_bigwheel_fans', $fanss);
            $fans['id'] = pdo_insertid();
            //增加人数，和浏览次数
            pdo_update('haoman_bigwheel_reply', array(
                'fansnum' => $reply['fansnum'] + 1,
                'viewnum' => $reply['viewnum'] + 1
            ), array(
                'id' => $reply['id']
            ));
        } else {
            
            if ($reply['ziliao'] == 1) {
                if (empty($fans['mobile'])) {
                    $this->message(array(
                        "success" => 5,
                        "msg" => '您还没有填兑奖信息!'
                    ), "");
                }
            } elseif ($reply['ziliao'] == 0) {
                //判断是否第一次中奖
                $award = pdo_fetchall("SELECT * FROM " . tablename('haoman_bigwheel_award') . " WHERE uniacid=" . $_AR['uniacid'] . " and rid = " . $rid . " and fansID=" . $fansID . " and from_user='" . $from_user . "' order by id desc");
                // $this->message(array("success" => 2, "msg" => $fans['tel']), "");
                if ($award != false) {
                    $awardone = $award[0];
                }
                
                //            $this->message(array("success" => 2, "msg" => "fadfdsfsd"), "");
                //判断是否填了手机号码
                if ($awardone && empty($fans['mobile'])) {
                    $this->message(array(
                        "success" => 5,
                        "msg" => '您还没有填兑奖信息!'
                    ), "");
                }
            }
            
            
            
            if ($fans['awardnum'] >= $reply['award_times'] && $reply['award_times'] != 0) {
                //     $this->message(array("success" => 2, "msg" => '您已经中过大奖了！不能再抽了'), "");
            }
            //增加浏览次数
            pdo_update('haoman_bigwheel_reply', array(
                'viewnum' => $reply['viewnum'] + 1
            ), array(
                'id' => $reply['id']
            ));
        }
        
        
        
        
        
        //查询分享赠送次数
        if ($reply['sharenum'] > 0) {
            
            if ($reply['most_num_times'] > 0 && $fans['sharenum'] >= 0) {
                $reply['most_num_times'] = $reply['most_num_times'] + $fans['sharenum'];
            }
        }
        //查询分享赠送次数
        //更新当日次数
        $nowtime = mktime(0, 0, 0);
        if ($fans['last_time'] < $nowtime) {
            $fans['todaynum']          = 0;
            $fans['today_award_times'] = 0;
        }
        if ($fans['todaynum'] >= $reply['most_num_times'] && $reply['most_num_times'] > 0 && $reply['number_times'] >= 0) {
            //$this->message('', '超过当日限制次数');
            $this->message(array(
                "success" => 2,
                "msg" => '您超过当日抽奖次数了，不能抽奖了!'
            ), "");
        }
        //所有奖品
        $gift = pdo_fetchall("select * from " . tablename('haoman_bigwheel_prize') . " where rid = :rid and uniacid=:uniacid order by Rand()", array(
            ':rid' => $rid,
            ':uniacid' => $uniacid
        ));
        $rate = 1;
        foreach ($gift as $giftxiao) {
            if ($giftxiao['awardstotal'] - $giftxiao['prizedraw'] >= 1) {
                $temp = explode('.', $giftxiao['probalilty']);
                $temp = pow(10, strlen($temp[1]));
                $rate = $temp < $rate ? $rate : $temp;
            }
        }
        $prize_arr = array();
        $isgift    = false;
        foreach ($gift as $row) {
            if ($row['awardstotal'] - $row['prizedraw'] >= 1 and floatval($row['awardspro']) > 0) {
                $item        = array(
                    'id' => $row['id'],
                    'prize' => $row['prizetype'],
                    'v' => $row['awardspro'] * $rate
                );
                $prize_arr[] = $item;
                $isgift      = true;
            }
            $zprizepro += $row['awardspro'];
        }
        //所有奖品
        if ($isgift) {
            $last_time = strtotime(date("Y-m-d", mktime(0, 0, 0)));
            //当天抽奖次数
            pdo_update('haoman_bigwheel_fans', array(
                'todaynum' => $fans['todaynum'] + 1,
                'last_time' => $last_time
            ), array(
                'id' => $fans['id']
            ));
            //开始抽奖咯
            foreach ($prize_arr as $key => $val) {
                $arr[$val['id']] = $val['v'];
            }
            $prizetype = $this->get_rand($arr); //根据概率获取奖项id
            
            
            
            
            if ($prizetype > 0) {
                
                $status      = 1;
                $consumetime = '';
                $awardinfo   = pdo_fetch("select * from " . tablename('haoman_bigwheel_prize') . " where  id='" . $prizetype . "'");
                
                $aid = pdo_fetch("select * from " . tablename('haoman_bigwheel_prize') . " where  rid = :rid and uniacid=:uniacid and ptype='lose'", array(
                    ':rid' => $rid,
                    ':uniacid' => $uniacid
                ));
                
                if ($fans['today_award_times'] >= $reply['today_award_times'] && $reply['today_award_times'] != 0) {
                    pdo_update('haoman_bigwheel_fans', array(
                        'todaynum' => $fans['todaynum'] + 1,
                        'last_time' => $last_time
                    ), array(
                        'id' => $fans['id']
                    ));
                    
                    if (!empty($aid['awardsimg'])) {
                        $aid['awardsimg'] = toimage($aid['awardsimg']);
                    }
                    $data = array(
                        'award' => $aid,
                        'success' => 1,
                        'height' => 245,
                        'prizetype' => $aid['id'],
                        'ptype' => $aid['ptype']
                    );
                    $this->message($data);
                }
                
                if ($fans['awardnum'] >= $reply['award_times'] && $reply['award_times'] != 0) {
                    pdo_update('haoman_bigwheel_fans', array(
                        'todaynum' => $fans['todaynum'] + 1,
                        'last_time' => $last_time
                    ), array(
                        'id' => $fans['id']
                    ));
                    
                    if (!empty($aid['awardsimg'])) {
                        $aid['awardsimg'] = toimage($aid['awardsimg']);
                    }
                    $data = array(
                        'award' => $aid,
                        'success' => 1,
                        'height' => 245,
                        'prizetype' => $aid['id'],
                        'ptype' => $aid['ptype']
                    );
                    $this->message($data);
                }
                
                switch ($awardinfo['ptype']) {
                    
                    case 'physical':
                        
                        //中奖记录保存
                        $insert = array(
                            'uniacid' => $uniacid,
                            'rid' => $rid,
                            'fansID' => $fansID,
                            'from_user' => $from_user,
                            'awardname' => $awardinfo['prizename'],
                            'awardsimg' => $awardinfo['awardsimg'],
                            'prizetype' => $prizetype,
                            'prize' => intval($prizetype),
                            'createtime' => time(),
                            'consumetime' => $consumetime,
                            'status' => $status
                        );
                        
                        $temp = pdo_insert('haoman_bigwheel_award', $insert);
                        
                        pdo_update('haoman_bigwheel_prize', array(
                            'prizedraw' => $awardinfo['prizedraw'] + 1
                        ), array(
                            'id' => $prizetype
                        ));
                        
                        //保存中奖人信息到fans中
                        pdo_update('haoman_bigwheel_fans', array(
                            'today_award_times' => $fans['today_award_times'] + 1,
                            'awardnum' => $fans['awardnum'] + 1,
                            'zhongjiang' => 1
                        ), array(
                            'id' => $fans['id']
                        ));
                        
                        break;
                    
                    case 'credit1':
                        
                        pdo_update('haoman_bigwheel_prize', array(
                            'prizedraw' => $awardinfo['prizedraw'] + 1
                        ), array(
                            'id' => $prizetype
                        ));
                        
                        $unisetting_s = uni_setting($uniacid, array(
                            'creditnames'
                        ));
                        foreach ($unisetting_s['creditnames'] as $key => $credit) {
                            if ($awardinfo['ptype'] == $key) {
                                $credit_names = $credit['title'];
                                break;
                            }
                        }
                        
                        mc_credit_update($fansID, $awardinfo['ptype'], $awardinfo['credit'], array(
                            $fansID,
                            '幸运大转盘活动抽中' . $awardinfo['credit'] . '个' . $credit_names
                        ));
                        
                        //中奖记录保存
                        $insert = array(
                            'uniacid' => $uniacid,
                            'rid' => $rid,
                            'fansID' => $fansID,
                            'from_user' => $from_user,
                            'awardname' => $awardinfo['prizename'],
                            'awardsimg' => $awardinfo['awardsimg'],
                            'prizetype' => $prizetype,
                            'prize' => $prizetype,
                            'createtime' => time(),
                            'consumetime' => time(),
                            'status' => 2
                        );
                        
                        $status      = 2;
                        $consumetime = time();
                        
                        $temp = pdo_insert('haoman_bigwheel_award', $insert);
                        
                        
                        //保存中奖人信息到fans中
                        pdo_update('haoman_bigwheel_fans', array(
                            'today_award_times' => $fans['today_award_times'] + 1,
                            'awardnum' => $fans['awardnum'] + 1,
                            'zhongjiang' => 1
                        ), array(
                            'id' => $fans['id']
                        ));
                        //保存中奖人信息到fans中
                        
                        break;
                    
                    case 'credit2':
                        
                        pdo_update('haoman_bigwheel_prize', array(
                            'prizedraw' => $awardinfo['prizedraw'] + 1
                        ), array(
                            'id' => $prizetype
                        ));
                        
                        $unisetting_s = uni_setting($uniacid, array(
                            'creditnames'
                        ));
                        foreach ($unisetting_s['creditnames'] as $key => $credit) {
                            if ($awardinfo['ptype'] == $key) {
                                $credit_names = $credit['title'];
                                break;
                            }
                        }
                        
                        mc_credit_update($fansID, $awardinfo['ptype'], $awardinfo['credit'], array(
                            $fansID,
                            '幸运大转盘活动抽中' . $awardinfo['credit'] . '个' . $credit_names
                        ));
                        //添加积分到粉丝数据库
                        
                        //中奖记录保存
                        $insert = array(
                            'uniacid' => $uniacid,
                            'rid' => $rid,
                            'fansID' => $fansID,
                            'from_user' => $from_user,
                            'awardname' => $awardinfo['prizename'],
                            'awardsimg' => $awardinfo['awardsimg'],
                            'prizetype' => $prizetype,
                            'prize' => $prizetype,
                            'createtime' => time(),
                            'consumetime' => time(),
                            'status' => 2
                        );
                        
                        $status      = 2;
                        $consumetime = time();
                        
                        $temp = pdo_insert('haoman_bigwheel_award', $insert);
                        
                        
                        //保存中奖人信息到fans中
                        pdo_update('haoman_bigwheel_fans', array(
                            'today_award_times' => $fans['today_award_times'] + 1,
                            'awardnum' => $fans['awardnum'] + 1,
                            'zhongjiang' => 1
                        ), array(
                            'id' => $fans['id']
                        ));
                        //保存中奖人信息到fans中
                        
                        break;
                    
                    case 'coupon':
                        
                        $rowid = $_RQ['cardrowid'];
                        
                        $prizetype = $_RQ['cardrowid'];
                        
                        pdo_update('haoman_bigwheel_prize', array(
                            'prizedraw' => $awardinfo['prizedraw'] + 1
                        ), array(
                            'id' => $prizetype
                        ));
                        
                        
                        //                        $rowid = 5;
                        $awardinfo = pdo_fetch("select * from " . tablename('haoman_bigwheel_prize') . " where  id='" . $rowid . "'");
                        
                        // $this->message(array("success" => 2, "msg" => $prizetype."|".$_RQ['cardrowid']."|".$awardinfo['prizename']), "");
                        
                        //中奖记录保存
                        $insert = array(
                            'uniacid' => $uniacid,
                            'rid' => $rid,
                            'fansID' => $fansID,
                            'from_user' => $from_user,
                            'awardname' => $awardinfo['prizename'],
                            'awardsimg' => $awardinfo['awardsimg'],
                            'prizetype' => $rowid,
                            'prize' => $prizetype,
                            'createtime' => time(),
                            'consumetime' => time(),
                            'status' => 2
                        );
                        
                        //                        $this->message(array("success" => 2, "msg" =>"4343214324324"), "");
                        
                        pdo_update('haoman_bigwheel_prize', array(
                            'prizedraw' => $awardinfo['prizedraw'] + 1
                        ), array(
                            'id' => $rowid
                        ));
                        
                        $temp = pdo_insert('haoman_bigwheel_award', $insert);
                        
                        
                        //保存中奖人信息到fans中
                        pdo_update('haoman_bigwheel_fans', array(
                            'today_award_times' => $fans['today_award_times'] + 1,
                            'awardnum' => $fans['awardnum'] + 1,
                            'zhongjiang' => 1
                        ), array(
                            'id' => $fans['id']
                        ));
                        //保存中奖人信息到fans中
                        
                        break;
                    
                    default:
                        
                }
                
                
                
                if (!empty($awardinfo['awardsimg'])) {
                    $awardinfo['awardsimg'] = toimage($awardinfo['awardsimg']);
                }
                
                if (empty($awardinfo['prizename'])) {
                    $awardinfo['prizename'] = "卡券ID设置错误";
                }
                
                
                $data = array(
                    'award' => $awardinfo,
                    'success' => 1,
                    'height' => 240,
                    'credit_now' => $credit_now,
                    'prizetype' => $prizetype,
                    'isfans' => $isfans,
                    'ptype' => $awardinfo['ptype']
                );
                
                $this->message($data);
            } else {
                
                //                $this->message(array("success" => 2, "msg" => "222222"), "");
                //                $this->message(array("success" => 2, "msg" => "fadfdsfdsf"), "");
                
                $data = array(
                    'msg' => '抱歉，您没有中奖，好遗憾，下次再来吧！',
                    'success' => 2,
                    'height' => 240
                );
                $this->message($data);
                
            }
        } else {
            
            //            $this->message(array("success" => 2, "msg" => "33333"), "");
            //没有奖品或没有资格的提示
            $data = array(
                'msg' => '没有奖品了，好遗憾，下次早点来吧！',
                'success' => 2,
                'height' => 180
            );
        }
        
        //     $this->message(array("success" => 2, "msg" => "444444"), "");
        
        $this->message($data);
?>
